package jlib.component.net
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	import jlib.utils.ObjectUtil;
	
	import mx.charts.chartClasses.StackedSeries;

	public class SingleLoader extends EventDispatcher implements ILoadable
	{
		private var loader:Loader;
		private var urlLoader:URLLoader;
		private var urlRequest:URLRequest;
		private var data:Object;
		/**
		 * 데이터 처리를 담당하는 메니저 입니다.
		 * Loader와 URLLoader 를 생성하는 기존에 방식에서 데이터 메니저가 페이지의 파일포멧 분석해서 
		 * Loader와 URLLoader 중 택일해 하나를 생성한 후 getData 메서드를 통해 값을 리턴합니다.  
		 */	
		public function SingleLoader(): void
		{
			urlRequest = new URLRequest();
		}
		/**
		 * 요청하는 페이지와 보낼 파라메터값입니다.
		 * @param url 주소
		 * @param param 파라메터
		 */		
		public function load(url:String, param:Object=null): void
		{
			var variables:URLVariables;
			if(param) variables = ObjectUtil.toURLVariable(param);
			if(hasCDATA(url)) url = changeCDATA(url);
			urlRequest.url = url;
			if(variables) urlRequest.data = variables;
			if(isResource(url)) loaderLoad(); 
			else urlLoaderLoad();
			//trace('urlRequest.data::', urlRequest.data, param);
		}
		/**
		 * 이벤트 Complete 후 받아오는 데이터 값 입니다.
		 * @return 참조형 포인트
		 * @includeExample ../../../../../bin/sample/DataManagerSample.as
		 */		
		public function getData(): * { return data; }
		
		private function urlLoaderLoad(): void
		{
			urlLoader = new URLLoader;
			urlLoader.addEventListener(Event.COMPLETE, urlLoaderLoaded );
			urlLoader.addEventListener(ProgressEvent.PROGRESS, dispatchEvent );
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, dispatchEvent );
			urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, dispatchEvent);
			urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, dispatchEvent);
			urlLoader.load(urlRequest);
		}
		
		private function urlLoaderLoaded(e:Object): void 
		{ 
			data = e.target.data;
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function loaderLoad(): void
		{
			loader = new Loader;
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderLoaded );
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, dispatchEvent );
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, dispatchEvent );
			loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, dispatchEvent);
			loader.load(urlRequest);
		}
		
		private function loaderLoaded(e:Object): void 
		{ 
			data = e.target.content;
			this.dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function isPage(url:String): Boolean
		{
			var result:Boolean;
			var format:String = url.substr(url.lastIndexOf(".")+1);
			if(format == 'html') result = true;
			else if(format == 'htm') result = true;
			else if(format == 'html') result = true;
			else if(format == 'xhtml') result = true;
			else if(format == 'jsp') result = true;
			else if(format == 'asp') result = true;
			else if(format == 'php') result = true;
			else if(format == 'xml') result = true;
			else if(format == 'do') result = true;
			trace('url:::',url ,' ,format:',format, ', isPage?', result);
			return result;
		}
		
		private function isResource(url:String): Boolean
		{
			var result:Boolean;
			var format:String = url.substr(url.lastIndexOf(".")+1);
			if(format == 'swf') result = true;
			else if(format == 'jpg') result = true;
			else if(format == 'bmp') result = true;
			else if(format == 'png') result = true;
			else if(format == 'gif') result = true;
			return result;
		}
		
		private function changeCDATA(str:String):String
		{
			if(!str) return null;
			var result:String = str.replace(/(<!\[CDATA\[)(.*?)(\]\]>)/g , '$2' );
			return result;
		}
		
		private function hasCDATA(str:String): Boolean
		{
			if(!str) return false;
			return (str.search('CDATA') != -1);
		}
		
	}//c
}